<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,100分)- 真正的密码（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>在一行中输入一个字符串数组&#xff0c;如果其中一个字符串的所有以索引0开头的子串在数组中都有&#xff0c;那么这个字符串就是潜在密码&#xff0c;</p> 
<p>在所有潜在密码中最长的是真正的密码&#xff0c;如果有多个长度相同的真正的密码&#xff0c;那么取字典序最大的为唯一的真正的密码&#xff0c;求唯一的真正的密码。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>无</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>无</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">h he hel hell hello o ok n ni nin ninj ninja</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">ninja</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"> <p>按要求&#xff0c;hello、ok、ninja都是潜在密码。</p> <p>检查长度&#xff0c;hello、ninja是真正的密码。</p> <p>检查字典序&#xff0c;ninja是唯一真正密码。</p> </td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">a b c d f</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">f</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"> <p>按要求&#xff0c;a b c d f 都是潜在密码。</p> <p>检查长度&#xff0c;a b c d f 是真正的密码。</p> <p>检查字典序&#xff0c;f是唯一真正密码。</p> </td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>我的解题思路如下&#xff1a;</p> 
<p>将输入的所有字符串先按照长度升序&#xff0c;如果长度相同&#xff0c;则按照字典序升序。</p> 
<p>这样最后一个字符串必然就是长度最长&#xff0c;字典序最大的&#xff0c;我们从最后一个字符串str开始&#xff1a;</p> 
<p>接下来&#xff0c;我们不停地截取str的子串&#xff0c;即如下闭区间的子串&#xff1a;</p> 
<p>0~str.length-2</p> 
<p>0~str.length-3</p> 
<p>0~str.length-4</p> 
<p>....</p> 
<p>0~2</p> 
<p>0~1</p> 
<p>0~0</p> 
<p>然后将在输入的所有字符串中查找每次截取的子串是否存在&#xff0c;如果多存在则返回当str作为题解&#xff0c;如果有一个不存在&#xff0c;则直接中断查找&#xff0c;开始下一个str</p> 
<p></p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const arr &#61; line.split(&#34; &#34;);

  console.log(getResult(arr));
});

function getResult(arr) {
  const set &#61; new Set(arr);
  arr.sort((a, b) &#61;&gt;
    a.length !&#61; b.length ? a.length - b.length : a &#61;&#61; b ? 0 : a &gt; b ? 1 : -1
  );

  outer: for (let i &#61; arr.length - 1; i &gt;&#61; 0; i--) {
    const str &#61; arr[i];

    for (let j &#61; str.length - 1; j &gt;&#61; 1; j--) {
      if (!set.has(str.slice(0, j))) continue outer;
    }

    return str;
  }
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String[] arr &#61; sc.nextLine().split(&#34; &#34;);
    System.out.println(getResult(arr));
  }

  public static String getResult(String[] arr) {
    HashSet&lt;String&gt; set &#61; new HashSet&lt;&gt;(Arrays.asList(arr));

    Arrays.sort(arr, (a, b) -&gt; a.length() !&#61; b.length() ? a.length() - b.length() : a.compareTo(b));

    outer:
    for (int i &#61; arr.length - 1; i &gt;&#61; 0; i--) {
      String str &#61; arr[i];

      for (int j &#61; str.length() - 1; j &gt;&#61; 1; j--) {
        if (!set.contains(str.substring(0, j))) continue outer;
      }

      return str;
    }

    return &#34;&#34;;
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
arr &#61; input().split()


# 算法入口
def getResult(arr):
    setArr &#61; set(arr)

    arr.sort(key&#61;lambda s: (len(s), [ord(c) for c in s]))

    for i in range(len(arr) - 1, -1, -1):
        s &#61; arr[i]
        flag &#61; True

        for j in range(len(s) - 1, 0, -1):
            if s[:j] not in setArr:
                flag &#61; False
                break

        if flag:
            return s

    return &#34;&#34;


# 算法调用
print(getResult(arr))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>